﻿using System.Reflection;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    public sealed class XmlUtils
    {
        private XmlUtils()
        {
        }

        public static object LoadObjectFromXml(string path, Type type)
        {
            object obj = null;
            using StreamReader streamReader = new StreamReader(path);
            string xml = streamReader.ReadToEnd();
            return XmlConvertor.XmlToObject(xml, type);
        }

        public static void SaveObjectToXml(string path, object obj)
        {
            string value = XmlConvertor.ObjectToXml(obj, toBeIndented: true);
            using StreamWriter streamWriter = new StreamWriter(path);
            streamWriter.Write(value);
        }

        public static string ReadFile(string path)
        {
            string empty = string.Empty;
            using StreamReader streamReader = new StreamReader(path);
            return streamReader.ReadToEnd();
        }

        public static string ReadFileFromEmbedded(string fileWholeName)
        {
            string empty = string.Empty;
            using TextReader textReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(fileWholeName));
            return textReader.ReadToEnd();
        }
    }
}
